package com.sx.manage.controller.operation;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.common.util.DateUtil;
import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.service.bank.IMBaseConsumerBankMapService;
import com.sx.manage.service.bank.IMConsumerBankMapService;
import com.sx.manage.service.operation.IMInsideAdjustOrderInfoService;
import com.sx.manage.service.operation.InsideAdjustOrderInfoService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.constants.MAdjustStatus;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.manage.mybatis.form.bank.MConsumerBankMapForm;
import com.sx.model.manage.mybatis.form.consumer.MPersonalConsumerNetworkForm;
import com.sx.model.manage.mybatis.form.sys_operation.MInsideAdjustOrderInfoForm;
import com.sx.model.manage.mybatis.vo.bank.MDropDownVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerBankMapVO;
import com.sx.model.manage.mybatis.vo.operation.MInsideAdjustOrderVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultDropDownListVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
/**
 * 系统运维-内部调账
 * @author zwm
 *
 */
@Controller
@RequestMapping("/inside_adjust_order")
public class MInsideAdjustOrderInfoController extends MBaseController{
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Resource(name="consumerBankMapServiceImpl")
	private IMConsumerBankMapService consumerBankMapServiceImpl;
	
	@Resource(name="mBaseConsumerBankMapServiceImpl")
	private IMBaseConsumerBankMapService mBaseConsumerBankMapServiceImpl;
	
	@Resource(name="mInsideAdjustOrderInfoServiceImpl")
	private IMInsideAdjustOrderInfoService mInsideAdjustOrderInfoServiceImpl;
	@Resource(name="insideAdjustOrderInfoService")
	private InsideAdjustOrderInfoService insideAdjustOrderInfoService;
	
	public Logger getLogger() {
		return logger;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public IMConsumerBankMapService getConsumerBankMapServiceImpl() {
		return consumerBankMapServiceImpl;
	}

	public void setConsumerBankMapServiceImpl(
			IMConsumerBankMapService consumerBankMapServiceImpl) {
		this.consumerBankMapServiceImpl = consumerBankMapServiceImpl;
	}

	public IMInsideAdjustOrderInfoService getmInsideAdjustOrderInfoServiceImpl() {
		return mInsideAdjustOrderInfoServiceImpl;
	}

	public void setmInsideAdjustOrderInfoServiceImpl(
			IMInsideAdjustOrderInfoService mInsideAdjustOrderInfoServiceImpl) {
		this.mInsideAdjustOrderInfoServiceImpl = mInsideAdjustOrderInfoServiceImpl;
	}

	public InsideAdjustOrderInfoService getInsideAdjustOrderInfoService() {
		return insideAdjustOrderInfoService;
	}

	public void setInsideAdjustOrderInfoService(
			InsideAdjustOrderInfoService insideAdjustOrderInfoService) {
		this.insideAdjustOrderInfoService = insideAdjustOrderInfoService;
	}

	/**
	 * 内部调账查询列表
	 * @return
	 */
	@RequestMapping(value="query_inside_adjust_view")
	public ViewAndModel query_viewInsideAdjustOrder(){
		ViewAndModel model=new ViewAndModel(request,cache);
		model.setViewName("sysMaintain/insideTransfer/checkTransfer");//修改ftl页面名称
		return model;
	}
	
	/**
	 * 内部调账查询列表
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/query_inside_adjust_page")
	 @ResponseBody
	public ResultListVO<MInsideAdjustOrderVO> search_inside_adjust(MInsideAdjustOrderInfoForm form){ 
		if ("".equals(form.getAdjust_type())) {
			form.setAdjust_type(null);
		}
		return mInsideAdjustOrderInfoServiceImpl.search_mInsideAdjustOrder(form);
	}
	
	/**
	 * 内部调账添加展示页
	 * @return
	 */
	@RequestMapping(value="inside_adjust_add_view")
	public ViewAndModel add_viewInsideAdjustOrder(){
		ViewAndModel model=new ViewAndModel(request,cache);
		model.setViewName("sysMaintain/insideTransfer/AddcheckTransfer");//修改ftl页面名称
		return model;
	}
	
	@RequestMapping(value="inside_adjust_save")
	@ResponseBody
	public ResultBooleanVO add_inside_adjust(MInsideAdjustOrderInfoForm form){
		if(null == form){
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		return mInsideAdjustOrderInfoServiceImpl.add_mInsideAdjustOrder(form, loginConsumer());
	}
	
	/**
	 * 技术审批
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/edit_inside_artisan_status")
	@ResponseBody
	public ResultBooleanVO  edit_artisan_status(MInsideAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		if ("yes".equals(form.getType())) {
			form.setArtisan_status(MAdjustStatus.ADJUST_SUCCSS);
		}else if("no".equals(form.getType())){
			form.setStatus(MAdjustStatus.ADJUST_FAIL);
			form.setArtisan_status(MAdjustStatus.ADJUST_FAIL);
		}
		form.setArtisan_time(DateUtil.getTimestamp());
		return mInsideAdjustOrderInfoServiceImpl.edit_mInsideAdjustOrder(form, loginConsumer());
	}
	@RequestMapping("/query_artisan_status")
	@ResponseBody
	public ResultBooleanVO query_artisan_status(MInsideAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		//查询数据状态和技术状态是否是撤销数据和审核状态变化
		return mInsideAdjustOrderInfoServiceImpl.query_artisan_status(form);
	}
	
	/**
	 * 财务审批
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/edit_inside_finance_status")
	@ResponseBody
	public ResultBooleanVO  edit_finance_status(MInsideAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		if ("yes".equals(form.getType())) {
			form.setFinance_status(MAdjustStatus.ADJUST_SUCCSS);
		}else if("no".equals(form.getType())){
			form.setStatus(MAdjustStatus.ADJUST_FAIL);
			form.setFinance_status(MAdjustStatus.ADJUST_FAIL);
		}
		form.setFinance_time(DateUtil.getTimestamp());
		return mInsideAdjustOrderInfoServiceImpl.edit_mInsideAdjustOrder(form, loginConsumer());
	}
	
	@RequestMapping("/query_finance_status")
	@ResponseBody
	public ResultBooleanVO query_finance_status(MInsideAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		//查询数据状态和技术状态是否是撤销数据和审核状态变化
		return mInsideAdjustOrderInfoServiceImpl.query_finance_status(form);
	}
	
	/**
	 * 产品审批
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/edit_inside_product_status")
	@ResponseBody
	public ResultBooleanVO  edit_product_status(MInsideAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		if ("yes".equals(form.getType())) {
			form.setProduct_status(MAdjustStatus.ADJUST_SUCCSS);
		}else if("no".equals(form.getType())){
			form.setStatus(MAdjustStatus.ADJUST_FAIL);
			form.setProduct_status(MAdjustStatus.ADJUST_FAIL);
		}
		form.setProduct_time(DateUtil.getTimestamp());
		return mInsideAdjustOrderInfoServiceImpl.edit_mInsideAdjustOrder(form, loginConsumer());
	}
	
	@RequestMapping("/query_product_status")
	@ResponseBody
	public ResultBooleanVO query_product_status(MInsideAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		//查询数据状态和技术状态是否是撤销数据和审核状态变化
		return mInsideAdjustOrderInfoServiceImpl.query_product_status(form);
	}
	
	/**
	 * 领导审批
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/edit_inside_boss_status")
	@ResponseBody
	public ResultBooleanVO  edit_boss_status(MInsideAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		if ("yes".equals(form.getType())) {
			form.setBoss_status(MAdjustStatus.ADJUST_SUCCSS);
			form.setStatus(MAdjustStatus.ADJUST_SUCCSS);
		}else if("no".equals(form.getType())){
			form.setBoss_status(MAdjustStatus.ADJUST_FAIL);
			form.setStatus(MAdjustStatus.ADJUST_FAIL);
		}
		form.setBoss_time(DateUtil.getTimestamp());
		return mInsideAdjustOrderInfoServiceImpl.edit_mInsideAdjustOrder(form, loginConsumer());
	}
	
	@RequestMapping("/query_boss_status")
	@ResponseBody
	public ResultBooleanVO query_boss_status(MInsideAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		//查询数据状态和技术状态是否是撤销数据和审核状态变化
		return mInsideAdjustOrderInfoServiceImpl.query_boss_status(form);
	}
	
	/**
	 * 撤销
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/edit_inside_revocation_status")
	@ResponseBody
	public ResultBooleanVO  edit_revocation_status(MInsideAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		ResultBooleanVO resultVO =  mInsideAdjustOrderInfoServiceImpl.query_revocation_status(form);
		if (!resultVO.isSuccess()) {
			return resultVO;
		}
		form.setStatus(MAdjustStatus.ADJUST_FAIL);
		form.setArtisan_status(MAdjustStatus.ADJUST_FAIL);
		form.setFinance_status(MAdjustStatus.ADJUST_FAIL);
		form.setProduct_status(MAdjustStatus.ADJUST_FAIL);
		form.setBoss_status(MAdjustStatus.ADJUST_FAIL);
		return mInsideAdjustOrderInfoServiceImpl.edit_mInsideAdjustOrder(form, loginConsumer());
	}
	
	/**
	 * 
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/query_user_bank_account")
	@ResponseBody
	public ResultDropDownListVO<MDropDownVO> ajax_bank_account_adjust(MPersonalConsumerNetworkForm form)throws IOException{
		return insideAdjustOrderInfoService.ajax_bank_account_adjust(form);
	}
	/** 查询子账户信息**/
	@RequestMapping("/query_user_bank_account/accountInfo")
	@ResponseBody
	public ResultObjectVO<MInsideAdjustOrderVO> accountInfo(MPersonalConsumerNetworkForm form)throws IOException{
		return insideAdjustOrderInfoService.getAccountInfoForQuery(form);
	}
	
	@RequestMapping("/change_account_status")
	@ResponseBody
	public ResultBooleanVO  change_account_status(MInsideAdjustOrderInfoForm form){
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		ResultObjectVO<MInsideAdjustOrderVO> resultObjVO = mInsideAdjustOrderInfoServiceImpl.search_ById(form);
		MInsideAdjustOrderVO mvo = (MInsideAdjustOrderVO)resultObjVO.getObjVo();
		if (null == mvo) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		if (!MAdjustStatus.ADJUST_SUCCSS.equals(mvo.getStatus())) {
			return new ResultBooleanVO("sj.m.data.account.status.error");
		}
		MConsumerBankMapForm bankMapForm = new MConsumerBankMapForm();
							 bankMapForm.setBank_account_no(mvo.getAccount());
							 bankMapForm.setChannel_status(MDataStatusType.DATA_AVAILABLE);
		List<MConsumerBankMapVO> volist = mBaseConsumerBankMapServiceImpl.query_consumer_bank_list(bankMapForm).getData();
		if (null == volist||volist.isEmpty()) {
			logger.info("子账户没有绑定银行卡，不能调账mvo={}",mvo);
			return new ResultBooleanVO(false,null,"子账户没有绑定银行卡，不能调账");
		}
		//控制调整账户余额
		ResultBooleanVO resultVO = insideAdjustOrderInfoService.change_account_status(form);
		if (resultVO.isSuccess()) {
			form.setStatus(MAdjustStatus.ADJUST_FINISH);
			form.setFinish_time(DateUtil.getTimestamp());
			mInsideAdjustOrderInfoServiceImpl.edit_mInsideAdjustOrder(form, loginConsumer());
		}else{
			form.setStatus(MAdjustStatus.ADJUST_FAIL);
			form.setFinish_time(DateUtil.getTimestamp());
			mInsideAdjustOrderInfoServiceImpl.edit_mInsideAdjustOrder(form, loginConsumer());
		}
		return resultVO;
	}

}
